package org.annoflow.aspect;

import java.util.HashMap;

import org.annoflow.filter.Filter;
import org.annoflow.filter.FilterPoint;

public aspect FilterAspect {
	private static HashMap<Class<? extends FilterPoint>, FilterPoint> _instances = 
		new HashMap<Class<? extends FilterPoint>, FilterPoint>();
	
	pointcut methodEnter(Filter f) :
		execution(@Filter * *(..)) &&
		@annotation(f);
	
	before(Filter f) : methodEnter(f) {
		Object[] arr = thisJoinPoint.getArgs();
		try {
			FilterPoint fp = _instances.get(f.value());
			if (fp == null) {
				synchronized (_instances) {
					if (!_instances.containsKey(f.value())) {
						_instances.put(f.value(), f.value().newInstance());
					}
				}
				fp = _instances.get(f.value());
			}
			fp.runFilter(arr);
		} catch (InstantiationException e) {
			//Do nothing
		} catch (IllegalAccessException e) {
			//Do nothing
		}
	}
}
